package com.example.apkmanage.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.apkmanage.R;
import com.example.apkmanage.holder.MainListViewHolder;
import com.fedorvlasov.lazylist.ImageLoader;
import com.ta.util.download.DownLoadCallback;
import com.ta.util.download.DownloadManager;

public class MainListViewAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context mContext;
	private OnClickListener downLoadOnClickListener;
	private JSONArray jsonList;
	private ListView listView;
	public ImageLoader imageLoader;

	public MainListViewAdapter(Context ctx, JSONArray listViewRes, OnClickListener downLoadOnClickListener, ListView listViewtemp) {
		this.mContext = ctx;
		this.mInflater = LayoutInflater.from(ctx);
		this.jsonList = listViewRes;
		this.listView = listViewtemp;
		this.downLoadOnClickListener = downLoadOnClickListener;

		imageLoader = new ImageLoader(ctx.getApplicationContext());

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return jsonList.length();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		MainListViewHolder viewHolder;

		convertView = null;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.index_item_listview, null);
			viewHolder = new MainListViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (MainListViewHolder) convertView.getTag();
		}

		JSONObject obj = null;
		try {
			obj = (JSONObject) jsonList.get(position);
			String url = obj.getString("vimagepath");
			imageLoader.DisplayImage(url, viewHolder.imageViewIcon);

			viewHolder.appName.setText(obj.getString("softName").toString());
			viewHolder.appSize.setText(obj.getString("vsize").toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (position % 2 == 0) {
			viewHolder.imgDownLoad.setBackgroundResource(R.drawable.down_btn_10);
			// viewHolder.progressBarLinearLayout.setVisibility(View.INVISIBLE);
		} else {
			// viewHolder.imgDownLoad.setBackgroundResource(R.drawable.down_btn_0);
			viewHolder.imgDownLoad.setBackgroundResource(R.drawable.pause_normal);
			viewHolder.appPriceFlag.setText("��ͣ");

		}
		// viewHolder.imgDownLoad.setOnClickListener(downLoadOnClickListener);
		return convertView;
	}

}